<?php
class LoaderEx extends Loader{
	function getClassPath($base_obj_type){
		$extract = explode('|',$base_obj_type);
		for($i = 0 ; $i < count($extract) -1;$i++){
			$classpath .= strtolower($extract[$i]) . '/';
		}
		return $classpath;
	}
	function getClassName($base_obj_type){
    if (strpos($base_obj_type,'|') !== false) {
      $extract = explode('|',$base_obj_type);
      $base_obj_type = $extract[count($extract)-1];
    }
    return $base_obj_type;
	}
	/**
     * Load a PNObject extended class from the given module. The given class name is
     * prefixed with 'PN' and underscores are removed to produce a proper class name.
	 * [add] Load PNObject from package.the package name seperate with "|" 
	 *       ex. Package|SubPackage|MyClass
     *
     * @param module        The module to load from
     * @param base_obj_type The base object type for which to load the class
     * @param array         If true, load the array class instead of the single-object class.
     * @param exitOnError   whether or not exit upon error (optional) (default=true)
     * @param prefix        Override parameter for the default PN prefix (default=PN)
     *
     * @return string The ClassName which was loaded from the file
     */
    function loadClassFromModule ($module, $base_obj_type, $array=false, $exitOnError=false, $prefix='PN')
    {
        if (!$module) {
            return pn_exit ("Invalid module specification [$module] ...");
        }

        if (!$base_obj_type) {
            return pn_exit ("Invalid base_obj_type specification [$base_obj_type] ...");
        }

        $prefix = (string)$prefix;
        $has_package = false;
        //load class from package
        
        if (strpos($base_obj_type,'|') !== false) {
          $classpath = null;
          $extract = explode('|',$base_obj_type);
          for($i = 0 ; $i < count($extract) -1;$i++){
            $classpath .= strtolower($extract[$i]) . '/';
          }
          
          $base_obj_type = $extract[count($extract)-1];
          $has_package =true;
        }

        if (strpos($base_obj_type,'_') !== false) {
            $c = $base_obj_type;
            $class = '';
            while (($p=strpos($c,'_')) !== false) {
                $class .= ucwords(substr ($c, 0, $p));
                $c = substr ($c, $p+1);
            }
            $class .= ucwords($c);
        } else {
            $class = ucwords($base_obj_type);
        }

        $class = $prefix . $class;
        if ($array) {
            $class .= 'Array';
        }
		
        // prevent unncessary reloading
        if (class_exists($class)) {
            return $class;
        }


        $classFiles = array();
        if ($has_package){
              $classFiles[] = "config/classes/$module/{$classpath}{$class}.class.php";
              $classFiles[] = "system/$module/classes/{$classpath}{$class}.class.php";
              $classFiles[] = "modules/$module/classes/{$classpath}{$class}.class.php";
        }else{
          $classFiles[] = "config/classes/$module/{$class}.class.php";
              $classFiles[] = "system/$module/classes/{$class}.class.php";
              $classFiles[] = "modules/$module/classes/{$class}.class.php";
        }
		
        foreach ($classFiles as $classFile) {
            $classFile = DataUtil::formatForOS($classFile);
            if (file_exists($classFile) && is_readable($classFile)) {
                if (Loader::includeOnce($classFile)) {
                    return $class;
                }

                if ($exitOnError) {
                    return pn_exit ("Unable to load class [$classFile] ...");
                }

                return false;
            }
        }

        return false;
    }
	
}